package com.zhongwang.business.module.contractManage.controller;

import com.zhongwang.business.base.BaseController;
import com.zhongwang.business.common.JsonResult;
import com.zhongwang.business.module.basic.model.DictEnumModel;
import com.zhongwang.business.module.basic.service.DictEnumService;
import com.zhongwang.business.module.contractManage.dao.ContractDao;
import com.zhongwang.business.module.contractManage.model.ConContractFundModel;
import com.zhongwang.business.module.contractManage.model.ConContractMainModel;
import com.zhongwang.business.module.contractManage.model.ContractModel;
import com.zhongwang.business.module.contractManage.service.ConContractMainService;
import com.zhongwang.business.module.contractManage.service.ContractService;
import com.zhongwang.business.module.fileManage.model.FileModel;
import com.zhongwang.business.module.taskManage.model.UnassignedTaskModel;
import com.zhongwang.business.module.taskManage.service.UnassignedTaskService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.jws.WebParam;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ContractController  extends BaseController {
    @Autowired
    private ContractService contractService;
    @Autowired
    private DictEnumService dictEnumService;
    @Autowired
    private ConContractMainService conContractMainService;
    @Autowired
    private ContractDao contractDao;
    @Autowired
    private UnassignedTaskService unassignedTaskService;
    /**
     * 待签合同列表页面的跳转
     */
    @GetMapping("/contractManage/unContractList")
    public String linkUnContractList() {
        return "/contractManage/unContractList";
    }

    /**
     * 已经下达的，需要合同；并且已经报价完毕的。
     */
    @ResponseBody
    @PostMapping("/contractManage/selUnContractList")
    public JsonResult selUnContractList(ContractModel model) {
        startPage();
        return jsonResult(contractService.unContractList(model));
    }


    /**
     * 创建/编辑合同页面的跳转
     */
    @GetMapping("/contractManage/contractInfo")
    public String linkCreateContractList(@RequestParam Integer quoteId,@RequestParam Integer conId,Model model) {
        /**
         * 查询出款项
         */
        model.addAttribute("ListDictEnum",conContractMainService.selConFun(conId));
        model.addAttribute("ListDictEnumAll",conContractMainService.selConFunOfAll(conId));

        model.addAttribute("conId", conId);
        ConContractMainModel contractMainModel =new ConContractMainModel();
        contractMainModel.setFkId(quoteId);
        if(conId!=null && conId>0) {
             contractMainModel = conContractMainService.selConContractMain(conId);
        }
        /**
         * ********************************项目编号***************************
         */
        ContractModel model1=new ContractModel();
        List<ContractModel> subList=contractDao.selectProjectById(quoteId);
        /**
         * 循环项目名称
         */
        String name="";

        if( subList.size()==1){
            name=name+subList.get(0).getCode();
        }else{
            for (int k= 0; k < subList.size(); k++) {
                name=name+subList.get(k).getCode()+";";
            }
        }
        /**
         * ********************************项目编号***************************
         */
        contractMainModel.setContName(name);
        model.addAttribute("contractMainModel", contractMainModel);
        /**
         * 查看驳回的最后一条
         */
        List<ConContractMainModel> appList= conContractMainService.selAppList(conId);
        if(appList!=null){
            if(appList.size()>0){
                if(!appList.get(0).getProjectName().equals("驳回")){
                    appList=new ArrayList<>();
                }
            }
        }
        model.addAttribute("appList",appList);


        return "/contractManage/contractInfo";
    }

    /**
     * 创建合同页面的跳转
     */
    @GetMapping("/contractManage/createContractList")
    public String linkCreateContractList(Integer quoteId, Model model) {
        DictEnumModel dictEnumModel=new DictEnumModel();
        dictEnumModel.setStatus(1);
        dictEnumModel.setGroupName("款项");
        model.addAttribute("ListDictEnum",dictEnumService.selectDictEnumByConditon(dictEnumModel));
        model.addAttribute("quoteId", quoteId);
        return "/contractManage/createContractList";
    }


    /**
     * 编辑合同
     * @param conId
     * @param model
     * @return
     */
    @GetMapping("/contractManage/editContractList")
    public String editContractList(Integer conId, Model model){
        /**
         * 查询出款项
         */
        model.addAttribute("ListDictEnum",conContractMainService.selConFun(conId));
        model.addAttribute("conId", conId);
        ConContractMainModel contractMainModel=conContractMainService.selConContractMain(conId);
        model.addAttribute("contractMainModel", contractMainModel);
        return "/contractManage/editContractList";
    }
    /**
     * 执行创建合同
     * @return
     */
    @ResponseBody
    @PostMapping("/contractManage/addContractInfo")
    public JsonResult addContractInfo(ConContractMainModel model){
        JsonResult jsonResult = new JsonResult();
        try {
            int flg =1;
            if(model.getId()!=null && model.getId()>0){
                flg = conContractMainService.updConContractMain(model);
            }else{
                flg = conContractMainService.addConContractMain(model);
            }
        /**
         * 2020.12.12
         * 根据是否是合作项目，修改下达任务表的isExpedited
         */
        UnassignedTaskModel un_model=new UnassignedTaskModel();
            un_model.setId(model.getFkId());
        if(model.getContType().equals("合作制造")){
            un_model.setIsExpedited(1);
        }else{
            un_model.setIsExpedited(0);
        }
        unassignedTaskService.updIsExpedited(un_model);

            if (flg >= 0) {
                jsonResult.setType(JsonResult.ResultType.SUCCESS);
                jsonResult.setMsg("保存成功");
            } else {
                jsonResult.setType(JsonResult.ResultType.ERROR);
                jsonResult.setMsg("保存失败");
            }
        }catch (Exception e){
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("保存异常");
        }
        return jsonResult;
    }
    /**
     * 上传附件
     * @param file
     * @return
     */
    @ResponseBody
    @PostMapping("/contractManage/uploadFile")
//    @RequiresPermissions({""})
    public JsonResult uploadFile(@RequestParam("file") MultipartFile[] file,@RequestParam("updQueId") Integer queId,@RequestParam("updConId") Integer mainId,@RequestParam("remark") String remark,@RequestParam("fileType") Integer fileType){
        JsonResult jsonResult=new JsonResult();
       try {
           int flg = conContractMainService.uploadFile(file, queId, mainId, remark, fileType);
           if (flg > 0) {
               jsonResult.setType(JsonResult.ResultType.SUCCESS);
               jsonResult.setData(flg);
               jsonResult.setMsg("上传成功");
           }
       }catch (Exception ex){
           jsonResult.setType(JsonResult.ResultType.ERROR);
           jsonResult.setData(mainId);
           jsonResult.setMsg("上传失败："+ex.getMessage());
       }
        return jsonResult;
    }

    /**
     * 上传附件 概算、预算、核算
     * @param file
     * @return
     */
    @ResponseBody
    @PostMapping("/contractManage/uploadFileGYH")
//    @RequiresPermissions({""})
    public JsonResult uploadFileGYH(@RequestParam("file") MultipartFile[] file,@RequestParam("fkId") Integer mainId,@RequestParam("remark") String remark,@RequestParam("fileType") Integer fileType){
        JsonResult jsonResult=new JsonResult();
        try {
            int flg = conContractMainService.uploadFileGYH(file, mainId, remark, fileType);
            if (flg > 0) {
                jsonResult.setType(JsonResult.ResultType.SUCCESS);
                jsonResult.setData(flg);
                jsonResult.setMsg("上传成功");
            }
        }catch (Exception ex){
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setData(mainId);
            jsonResult.setMsg("上传失败："+ex.getMessage());
        }
        return jsonResult;
    }


    /**
     * 编辑数据
     * @param file
     * @return
     */
    @ResponseBody
    @PostMapping("/file/editFileInfo")
    public JsonResult editFileInfo(@RequestParam("file") MultipartFile[] file, Integer mainId,Integer fileId,String fileComment,Integer fileType){
        return jsonResult(conContractMainService.editFileInfo(file, mainId,fileId,fileComment, fileType));
    }


    /**
     * 合同提交审核
     */
    @ResponseBody
    @PostMapping("/contractManage/contractAppInfo")
    @Transactional
    public JsonResult contractAppInfo(@RequestParam Integer conId) {
        JsonResult jsonResult=new JsonResult();
        int a=conContractMainService.contractAppInfo(conId);
        if(a>0){
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("提交成功");
        }else{
            jsonResult.setType(JsonResult.ResultType.ERROR);
        }
        return jsonResult;
    }


    /**
     * 合同提交审核
     */
    @ResponseBody
    @PostMapping("/contractManage/deleteApp")
    @Transactional
    public JsonResult deleteApp(@RequestParam Integer conId) {
        JsonResult jsonResult=new JsonResult();
        int a=conContractMainService.deleteApp(conId);
        if(a>0){
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("删除成功");
        }else{
            jsonResult.setType(JsonResult.ResultType.ERROR);
        }
        return jsonResult;
    }

    /**
     * 已经下达的，需要合同;并且已经报价完毕的。
     */
    @ResponseBody
    @PostMapping("/contractManage/unAppContractList")
    public JsonResult unAppContractList(ConContractMainModel model) {
        startPage();
        return jsonResult(conContractMainService.unAppContractList(model));
    }
    /**
     * 待审核合同列表页面的跳转
     */
    @GetMapping("/contractManage/unAppConList")
    public String linkUnAppConList() {
        return "/contractManage/unAppConList";
    }

    /**
     * 审核合同页面的跳转
     */
    @GetMapping("/contractManage/contractApprove")
    public String LookCreateContractList(@RequestParam Integer quoteId,@RequestParam Integer conId, @RequestParam Integer processId ,Model model) {
        /**
         * 查询出款项
         */
        model.addAttribute("ListDictEnum",conContractMainService.selConFun(conId));
        model.addAttribute("ListDictEnumAll",conContractMainService.selConFunOfAll(conId));
        model.addAttribute("conId", conId);
        ConContractMainModel contractMainModel =new ConContractMainModel();
        contractMainModel.setFkId(quoteId);
        if(conId!=null && conId>0) {
            contractMainModel = conContractMainService.selConContractMain(conId);
        }
        /**
         * ********************************项目编号***************************
         */
        ContractModel model1=new ContractModel();
        List<ContractModel> subList=contractDao.selectProjectById(quoteId);
        /**
         * 循环项目名称
         */
        String name="";
        if(subList.size()==1){
            name = name + subList.get(0).getCode();
        }else {
            for (int k = 0; k < subList.size(); k++) {
                name = name + subList.get(k).getCode() + ";";
            }
        }
        /**
         * ********************************项目编号***************************
         */
        contractMainModel.setContName(name);
        model.addAttribute("contractMainModel", contractMainModel);
        model.addAttribute("processId",processId);

        model.addAttribute("appList",conContractMainService.selAppList(conId));
        return "/contractManage/contractApprove";
    }
    /**
     * 合同查看详细页面的跳转
     */
    @GetMapping("/contractManage/showDetail")
    public String showDetail(@RequestParam Integer quoteId,@RequestParam Integer conId ,Model model) {
        /**
         * 查询出款项
         */
        model.addAttribute("ListDictEnum",conContractMainService.selConFun(conId));
        model.addAttribute("ListDictEnumAll",conContractMainService.selConFunOfAll(conId));
        model.addAttribute("conId", conId);
        ConContractMainModel contractMainModel =new ConContractMainModel();
        contractMainModel.setFkId(quoteId);
        if(conId!=null && conId>0) {
            contractMainModel = conContractMainService.selConContractMain(conId);
        }
        /**
         * ********************************项目编号***************************
         */
        ContractModel model1=new ContractModel();
        List<ContractModel> subList=contractDao.selectProjectById(quoteId);
        /**
         * 循环项目名称
         */
        String name="";
        if(subList.size()==1){
            name = name + subList.get(0).getCode();
        }else {
            for (int k = 0; k < subList.size(); k++) {
                name = name + subList.get(k).getCode() + ";";
            }
        }

        /**
         * ********************************项目编号***************************
         */
        contractMainModel.setContName(name);
        model.addAttribute("contractMainModel", contractMainModel);

        return "/contractManage/showDetail";
    }

    /**
     * 合同审核
     */
    @ResponseBody
    @PostMapping("/contractManage/contractAppInfoByUserRole")
    @Transactional
    public JsonResult contractAppInfoByUserRole(ConContractMainModel model) {
        JsonResult jsonResult=new JsonResult();
        int a=1;
        a=conContractMainService.approveInfoByBllId(model);
        if(a>0){
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("提交成功");
        }else{
            jsonResult.setType(JsonResult.ResultType.ERROR);
        }
        return jsonResult;
    }
    /**
     * 合同台账页面跳转
     */
    @GetMapping("/contractManage/contractList")
    public String linkContractList() {
        return "/contractManage/contractList";
    }

    /**
     * 已经下达的，需要合同;并且已经报价完毕的。
     */
    @ResponseBody
    @PostMapping("/contractManage/selContractList")
    public JsonResult selContractList(ConContractMainModel model) {
        startPage();
        return jsonResult(conContractMainService.selContractList(model));
    }


    /**
     * 合同待提交页面
     */
    @GetMapping("/contractManage/contractSubmit")
    public String linkContractSubmit() {
        return "/contractManage/contractSubmit";
    }
    /**
     * 已经下达的，需要合同;并且已经报价完毕的。
     */
    @ResponseBody
    @PostMapping("/contractManage/selContractSubmitList")
    public JsonResult selContractSubmitList(ConContractMainModel model) {
//        startPage();
        return jsonResult(contractService.unContractSubList(model));
    }

    /**
     * 合同查询页面跳转
     */
    @GetMapping("/contractManage/contractSearch")
    public String linkContractSearch() {
        return "/contractManage/contractSearch";
    }

    /**
     * 合同查询页面
     */
    @ResponseBody
    @PostMapping("/contractManage/selAllContract")
    public JsonResult selAllContract(ContractModel model) {
        startPage();
        return jsonResult(conContractMainService.selAllContract(model));
    }

    /**
     * 增补协议 创建/编辑 页面的跳转
     */
    @GetMapping("/contractManage/contractInfoAppend")
    public String linkContractInfoAppend(@RequestParam Integer quoteId,@RequestParam Integer conId,@RequestParam Integer parentId,Model model) {
        List<ConContractMainModel> appList= conContractMainService.selAppList(conId);
        if(appList!=null){
            if(appList.size()>0){
                if(!appList.get(0).getProjectName().equals("驳回")){
                    appList=new ArrayList<>();
                }
            }
        }
        model.addAttribute("appList",appList);
        model.addAttribute("conId", conId);
        /**
         * 查询出款项
         */
        if(conId==0 ||conId==null){
            conId=parentId;
        }
        model.addAttribute("ListDictEnum",conContractMainService.selConFun(conId));
        model.addAttribute("ListDictEnumAll",conContractMainService.selConFunOfAll(conId));


        ConContractMainModel contractMainModel =new ConContractMainModel();
        contractMainModel.setFkId(quoteId);
        if(conId!=null && conId>0) {
            contractMainModel = conContractMainService.selConContractMain(conId);
        }
        /**
         * ********************************项目编号***************************
         */
        ContractModel model1=new ContractModel();
        List<ContractModel> subList=contractDao.selectProjectById(quoteId);
        /**
         * 循环项目名称
         */
        String name="";
        if( subList.size()==1){
            name=name+subList.get(0).getCode();
        }else{
            for (int k= 0; k < subList.size(); k++) {
                name=name+subList.get(k).getCode()+";";
            }
        }
        /**
         * ********************************项目编号***************************
         */
        contractMainModel.setContName(name);
        /**
         * 父级报价单id
         */
        contractMainModel.setParentId(parentId);
        model.addAttribute("contractMainModel", contractMainModel);

        return "/contractManage/addContractInfo";
    }
    /**
     * 合同查看详细页面的跳转,合并了 合同和增补合同
     */
    @GetMapping("/contractManage/lookContract")
    public String lookContract(@RequestParam Integer quoteId,@RequestParam Integer conId,@RequestParam Integer parentId,Model model) {
        /**
         * 查看合同时候，如果是增补合同，那么主单信息显示的是原合同的信息。
         */
        if(parentId>0){
            conId=parentId;
        }
        /**
         * 查询出款项
         */
        model.addAttribute("ListDictEnum",conContractMainService.selConFunEnd(conId));
        model.addAttribute("ListDictEnumAll",conContractMainService.selConFunOfAllEnd(conId));
        model.addAttribute("conId", conId);
        ConContractMainModel contractMainModel =new ConContractMainModel();
        contractMainModel.setFkId(quoteId);
        if(conId!=null && conId>0) {
            contractMainModel = conContractMainService.selConContractMain(conId);
        }
        /**
         * ********************************项目编号***************************
         */
        ContractModel model1=new ContractModel();
        List<ContractModel> subList=contractDao.selectProjectById(quoteId);
        /**
         * 循环项目名称
         */
        String name="";
        if(subList.size()==1){
            name = name + subList.get(0).getCode();
        }else {
            for (int k = 0; k < subList.size(); k++) {
                name = name + subList.get(k).getCode() + ";";
            }
        }
        /**
         * ********************************项目编号***************************
         */
        contractMainModel.setContName(name);
        model.addAttribute("contractMainModel", contractMainModel);
        /**
         * 增补协议部分
         */
        List<ConContractMainModel>  list_app=new ArrayList<>();
        List<ConContractMainModel> tempList=conContractMainService.selectAppConList(conId);
        if(tempList!=null && tempList.size()>=2){
            for(int i=0;i<tempList.size()-1;i++){
                ConContractMainModel temp=new ConContractMainModel();

                if(tempList.get(i+1).getPartMethod()!=""){
                    temp.setPartMethod(tempList.get(i+1).getPartMethod());
                }
                if(!tempList.get(i).getContType().equals(tempList.get(i+1).getContType())){
                    temp.setContType(tempList.get(i+1).getContType());
                }
                if(!tempList.get(i).getSellCode().equals(tempList.get(i+1).getSellCode())){
                    temp.setSellCode(tempList.get(i+1).getSellCode());
                }
                if(!tempList.get(i).getBuyCode().equals(tempList.get(i+1).getBuyCode())){
                    temp.setBuyCode(tempList.get(i+1).getBuyCode());
                }
                if(!tempList.get(i).getContWay().equals(tempList.get(i+1).getContWay())){
                    temp.setContWay(tempList.get(i+1).getContWay());
                }
                if(!tempList.get(i).getContAddress().equals(tempList.get(i+1).getContAddress())){
                    temp.setContAddress(tempList.get(i+1).getContAddress());
                }
                if(!tempList.get(i).getContName().equals(tempList.get(i+1).getContName())){
                    temp.setContName(tempList.get(i+1).getContName());
                }
                if(!tempList.get(i).getContPartner().equals(tempList.get(i+1).getContPartner())){
                    temp.setContPartner(tempList.get(i+1).getContPartner());
                }
                if(!tempList.get(i).getCnt().equals(tempList.get(i+1).getCnt())){
                    temp.setCnt(tempList.get(i+1).getCnt());
                }
                if(!tempList.get(i).getSaveCode().equals(tempList.get(i+1).getSaveCode())){
                    temp.setSaveCode(tempList.get(i+1).getSaveCode());
                }
                if(!tempList.get(i).getTotalMethod().equals(tempList.get(i+1).getTotalMethod())){
                    temp.setTotalMethod(tempList.get(i+1).getTotalMethod());
                }
                if(!tempList.get(i).getTotalMoney().equals(tempList.get(i+1).getTotalMoney())){
                    temp.setTotalMoney(tempList.get(i+1).getTotalMoney());
                }
                if(!tempList.get(i).getPartMoney().equals(tempList.get(i+1).getPartMoney())){
                    temp.setPartMoney(tempList.get(i+1).getPartMoney());
                }
                if(temp!=null) {
                    list_app.add(temp);
                }
            }
        }
        model.addAttribute("ListAppContractList", list_app);

        return "/contractManage/lookContract";
    }

    /**
     * 增补协议页面跳转
     */
    @GetMapping("/contractManage/appendList")
    public String linkAppendList() {
        return "/contractManage/appendList";
    }

    /**
     * 待创建增补协议列表。
     */
    @ResponseBody
    @PostMapping("/contractManage/appendContractList")
    public JsonResult appendContractList(ConContractMainModel model) {
        startPage();
        return jsonResult(conContractMainService.appendContractList(model));
    }
    /**
     * 审核合同页面的跳转
     */
    @GetMapping("/contractManage/contractApproveNew")
    public String LookCreateContractNewList(@RequestParam Integer quoteId,@RequestParam Integer conId, @RequestParam Integer processId ,@RequestParam Integer parentId,Model model) {
//        /**
//         * 查询出款项
//         */
//        model.addAttribute("ListDictEnum",conContractMainService.selConFun(conId));
//        model.addAttribute("ListDictEnumAll",conContractMainService.selConFunOfAll(conId));
//        model.addAttribute("conId", conId);
//        ConContractMainModel contractMainModel =new ConContractMainModel();
//        contractMainModel.setFkId(quoteId);
//        if(conId!=null && conId>0) {
//            contractMainModel = conContractMainService.selConContractMain(conId);
//        }
//        /**
//         * ********************************项目编号***************************
//         */
//        ContractModel model1=new ContractModel();
//        List<ContractModel> subList=contractDao.selectProjectById(quoteId);
//        /**
//         * 循环项目名称
//         */
//        String name="";
//        for (int k= 0; k < subList.size(); k++) {
//            name=name+subList.get(k).getCode()+";";
//        }
//        /**
//         * ********************************项目编号***************************
//         */
//        contractMainModel.setContName(name);
//        model.addAttribute("contractMainModel", contractMainModel);
        model.addAttribute("processId",processId);
        /**
        * 查询出款项
        */
        model.addAttribute("ListDictEnum",conContractMainService.selConFun(conId));
        model.addAttribute("ListDictEnumAll",conContractMainService.selConFunOfAll(conId));
        List<ConContractMainModel> appList= conContractMainService.selAppList(conId);
        if(appList!=null){
            if(appList.size()>0){
                if(!appList.get(0).getProjectName().equals("驳回")){
                    appList=new ArrayList<>();
                }
            }
        }
        model.addAttribute("appList",appList);
        model.addAttribute("conId", conId);
        ConContractMainModel contractMainModel =new ConContractMainModel();
        contractMainModel.setFkId(quoteId);


        /**
         * 查看合同时候，如果是增补合同，那么主单信息显示的是原合同的信息。
         */
        if(parentId>0){
            conId=parentId;
        }
        if(conId!=null && conId>0) {
            contractMainModel = conContractMainService.selConContractMain(conId);
        }


        /**
         * ********************************项目编号***************************
         */
        ContractModel model1=new ContractModel();
        List<ContractModel> subList=contractDao.selectProjectById(quoteId);
        /**
         * 循环项目名称
         */
        String name="";
        if(subList.size()==1){
            name = name + subList.get(0).getCode();
        }else {
            for (int k = 0; k < subList.size(); k++) {
                name = name + subList.get(k).getCode() + ";";
            }
        }

        /**
         * ********************************项目编号***************************
         */
        contractMainModel.setContName(name);
        model.addAttribute("contractMainModel", contractMainModel);
        /**
         * 增补协议部分
         */
        List<ConContractMainModel>  list_app=new ArrayList<>();
        List<ConContractMainModel> tempList=conContractMainService.selectAppConList(conId);
        if(tempList!=null && tempList.size()>=2){
            for(int i=0;i<tempList.size()-1;i++){
                ConContractMainModel temp=new ConContractMainModel();

                if(tempList.get(i+1).getPartMethod()!=""){
                    temp.setPartMethod(tempList.get(i+1).getPartMethod());
                }
                if(!tempList.get(i).getContType().equals(tempList.get(i+1).getContType())){
                    temp.setContType(tempList.get(i+1).getContType());
                }
                if(!tempList.get(i).getSellCode().equals(tempList.get(i+1).getSellCode())){
                    temp.setSellCode(tempList.get(i+1).getSellCode());
                }
                if(!tempList.get(i).getBuyCode().equals(tempList.get(i+1).getBuyCode())){
                    temp.setBuyCode(tempList.get(i+1).getBuyCode());
                }
                if(!tempList.get(i).getContWay().equals(tempList.get(i+1).getContWay())){
                    temp.setContWay(tempList.get(i+1).getContWay());
                }
                if(!tempList.get(i).getContAddress().equals(tempList.get(i+1).getContAddress())){
                    temp.setContAddress(tempList.get(i+1).getContAddress());
                }
                if(!tempList.get(i).getContName().equals(tempList.get(i+1).getContName())){
                    temp.setContName(tempList.get(i+1).getContName());
                }
                if(!tempList.get(i).getContPartner().equals(tempList.get(i+1).getContPartner())){
                    temp.setContPartner(tempList.get(i+1).getContPartner());
                }
                if(!tempList.get(i).getCnt().equals(tempList.get(i+1).getCnt())){
                    temp.setCnt(tempList.get(i+1).getCnt());
                }
                if(!tempList.get(i).getSaveCode().equals(tempList.get(i+1).getSaveCode())){
                    temp.setSaveCode(tempList.get(i+1).getSaveCode());
                }
                if(!tempList.get(i).getTotalMethod().equals(tempList.get(i+1).getTotalMethod())){
                    temp.setTotalMethod(tempList.get(i+1).getTotalMethod());
                }
                if(!tempList.get(i).getTotalMoney().equals(tempList.get(i+1).getTotalMoney())){
                    temp.setTotalMoney(tempList.get(i+1).getTotalMoney());
                }
                if(!tempList.get(i).getPartMoney().equals(tempList.get(i+1).getPartMoney())){
                    temp.setPartMoney(tempList.get(i+1).getPartMoney());
                }
                if(temp!=null) {
                    list_app.add(temp);
                }
            }
        }
        model.addAttribute("ListAppContractList", list_app);


        return "/contractManage/contractApproveNew";
    }

    /**
     * 审核合同页面的跳转
     */
    @GetMapping("/contractManage/contractApproveDetail")
    public String LookContractApproveDetail(@RequestParam Integer quoteId,@RequestParam Integer conId, @RequestParam Integer processId ,@RequestParam Integer parentId,Model model) {

        model.addAttribute("processId",processId);
        /**
         * 查询出款项
         */
        model.addAttribute("ListDictEnum",conContractMainService.selConFun(conId));
        model.addAttribute("ListDictEnumAll",conContractMainService.selConFunOfAll(conId));
        //审核查看详细页面，审核的过程信息不看。
        //        model.addAttribute("appList",conContractMainService.selAppList(conId));
        model.addAttribute("conId", conId);
        ConContractMainModel contractMainModel =new ConContractMainModel();
        contractMainModel.setFkId(quoteId);


        /**
         * 查看合同时候，如果是增补合同，那么主单信息显示的是原合同的信息。
         */
        if(parentId>0){
            conId=parentId;
        }
        if(conId!=null && conId>0) {
            contractMainModel = conContractMainService.selConContractMain(conId);
        }


        /**
         * ********************************项目编号***************************
         */
        ContractModel model1=new ContractModel();
        List<ContractModel> subList=contractDao.selectProjectById(quoteId);
        /**
         * 循环项目名称
         */
        String name="";
        if(subList.size()==1){
            name = name + subList.get(0).getCode();
        }else {
            for (int k = 0; k < subList.size(); k++) {
                name = name + subList.get(k).getCode() + ";";
            }
        }
        /**
         * ********************************项目编号***************************
         */
        contractMainModel.setContName(name);
        model.addAttribute("contractMainModel", contractMainModel);
        /**
         * 增补协议部分
         */
        List<ConContractMainModel>  list_app=new ArrayList<>();
        List<ConContractMainModel> tempList=conContractMainService.selectAppConList(conId);
        if(tempList!=null && tempList.size()>=2){
            for(int i=0;i<tempList.size()-1;i++){
                ConContractMainModel temp=new ConContractMainModel();

                if(tempList.get(i+1).getPartMethod()!=""){
                    temp.setPartMethod(tempList.get(i+1).getPartMethod());
                }
                if(!tempList.get(i).getContType().equals(tempList.get(i+1).getContType())){
                    temp.setContType(tempList.get(i+1).getContType());
                }
                if(!tempList.get(i).getSellCode().equals(tempList.get(i+1).getSellCode())){
                    temp.setSellCode(tempList.get(i+1).getSellCode());
                }
                if(!tempList.get(i).getBuyCode().equals(tempList.get(i+1).getBuyCode())){
                    temp.setBuyCode(tempList.get(i+1).getBuyCode());
                }
                if(!tempList.get(i).getContWay().equals(tempList.get(i+1).getContWay())){
                    temp.setContWay(tempList.get(i+1).getContWay());
                }
                if(!tempList.get(i).getContAddress().equals(tempList.get(i+1).getContAddress())){
                    temp.setContAddress(tempList.get(i+1).getContAddress());
                }
                if(!tempList.get(i).getContName().equals(tempList.get(i+1).getContName())){
                    temp.setContName(tempList.get(i+1).getContName());
                }
                if(!tempList.get(i).getContPartner().equals(tempList.get(i+1).getContPartner())){
                    temp.setContPartner(tempList.get(i+1).getContPartner());
                }
                if(!tempList.get(i).getCnt().equals(tempList.get(i+1).getCnt())){
                    temp.setCnt(tempList.get(i+1).getCnt());
                }
                if(!tempList.get(i).getSaveCode().equals(tempList.get(i+1).getSaveCode())){
                    temp.setSaveCode(tempList.get(i+1).getSaveCode());
                }
                if(!tempList.get(i).getTotalMethod().equals(tempList.get(i+1).getTotalMethod())){
                    temp.setTotalMethod(tempList.get(i+1).getTotalMethod());
                }
                if(!tempList.get(i).getTotalMoney().equals(tempList.get(i+1).getTotalMoney())){
                    temp.setTotalMoney(tempList.get(i+1).getTotalMoney());
                }
                if(!tempList.get(i).getPartMoney().equals(tempList.get(i+1).getPartMoney())){
                    temp.setPartMoney(tempList.get(i+1).getPartMoney());
                }
                if(temp!=null) {
                    list_app.add(temp);
                }
            }
        }
        model.addAttribute("ListAppContractList", list_app);


        return "/contractManage/contractApproveDetail";
    }
    /**
     * 合同审核
     */
    @ResponseBody
    @PostMapping("/contractManage/updContWay")
    public JsonResult updContWay(@RequestParam Integer id,@RequestParam String contWay) {
        JsonResult jsonResult=new JsonResult();
        int a=1;
        a=conContractMainService.updContWay(id,contWay);
        if(a>0){
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("提交成功");
        }else{
            jsonResult.setType(JsonResult.ResultType.ERROR);
        }
        return jsonResult;
    }
    /**
     * 查询人员的集合
     * @return
     */
    @ResponseBody
    @PostMapping("/contractManage/userList")
    public JsonResult selUser(){
        return jsonResult(conContractMainService.selUser());
    }

    /**
     * 修改人
     * @return
     */
    @ResponseBody
    @PostMapping("/contractManage/updateName")
    public JsonResult updateName(@RequestParam int conid,@RequestParam String name){
        JsonResult jsonResult=new JsonResult();
        ConContractMainModel model=new ConContractMainModel();
        model.setId(conid);
        model.setName(name);
        if(conContractMainService.updateName(model)>0){
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        return jsonResult;
    }

}
